package util;

import org.apache.log4j.Logger;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DbUtils {

    private static final Logger logger = Logger.getLogger(DbUtils.class);

    public static void closeQuietly(ResultSet rs) {
        try {
            if (rs != null) {
                rs.close();
            }
        } catch (SQLException e) {
            logger.error(e.getMessage(), e);
        }
    }

    public static void closeQuietly(PreparedStatement pres) {
        try {
            if (pres != null) {
                pres.close();
            }
        } catch (SQLException e) {
            logger.error(e.getMessage(), e);
        }
    }

    public static void closeQuietly(Connection conn) {
        try {
            if (conn != null) {
                conn.close();
            }
        } catch (SQLException e) {
            logger.error(e.getMessage(), e);
        }
    }

    public static void closeQuietly(PreparedStatement pres, Connection conn) {
        try {
            closeQuietly(pres);
        } finally {
            closeQuietly(conn);
        }
    }

    public static void closeQuietly(ResultSet rs, PreparedStatement pres, Connection conn) {
        try {
            closeQuietly(rs);
        } finally {
            try {
                closeQuietly(pres);
            } finally {
                closeQuietly(conn);
            }
        }
    }
}
